package com.edee.bigday.commodity.app;

import com.edee.bigday.commodity.entity.AttrEntity;
import com.edee.bigday.commodity.entity.AttrGroupEntity;
import com.edee.bigday.commodity.service.AttrAttrgroupRelationService;
import com.edee.bigday.commodity.service.AttrGroupService;
import com.edee.bigday.commodity.service.AttrService;
import com.edee.bigday.commodity.service.CategoryService;
import com.edee.bigday.commodity.vo.AttrGroupRelationVo;
import com.edee.bigday.commodity.vo.AttrGroupWithAttrsVo;
import com.edee.common.utils.PageUtils;
import com.edee.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author edee
 * @email 421763094@qq.com
 * @date 2020-12-09 23:08:51
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;


    // /commodity/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
//    public R addRelation(@RequestBody AttrGroupRelationVo[] vos){
    //上面的List<AttrGroupRelationVo>和AttrGroupRelationVo[]均可。
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    //  /commodity/attrgroup/{catalogId}/withattr
    @GetMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catalogId") Long catalogId){
        //1、查出当前分类下的所有属性分组：
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatId(catalogId);
        return R.ok().put("data",vos);
    }

    // /commodity/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.getRaletionAttr(attrgroupId);
        return R.ok().put("data", attrEntities);
    }

    // /commodity/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catalogId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
//                  @PathParam("catalogId") Long catalogId) { //第一次将@PathVariable写成了@PathParam
                  @PathVariable("catalogId") Long catalogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catalogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catalogId = attrGroup.getCatalogId();
        Long[] path = categoryService.findCatalogPath(catalogId);
        attrGroup.setCatalogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }


    // /commodity/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {  //参数也可以是一个List
        //@RequestBody将自定义对象数据封装为json格式
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}